package com.ghsc.yigou.live.ui

import android.Manifest
import android.content.Intent
import android.text.TextUtils
import android.util.Log
import com.alibaba.sdk.android.oss.ClientException
import com.alibaba.sdk.android.oss.OSS
import com.alibaba.sdk.android.oss.ServiceException
import com.alibaba.sdk.android.oss.callback.OSSCompletedCallback
import com.alibaba.sdk.android.oss.callback.OSSProgressCallback
import com.alibaba.sdk.android.oss.internal.OSSAsyncTask
import com.alibaba.sdk.android.oss.model.PutObjectRequest
import com.alibaba.sdk.android.oss.model.PutObjectResult
import com.alivc.auicommon.common.base.callback.Callbacks
import com.alivc.auicommon.common.roombase.Const
import com.alivc.auimessage.listener.InteractionCallback
import com.alivc.auimessage.model.base.InteractionError
import com.aliyun.aliinteraction.uikit.uibase.util.LastLiveSp
import com.aliyun.auiappserver.AppServerTokenManager
import com.aliyun.auipusher.LiveRole
import com.aliyun.auipusher.config.AliLiveMediaStreamOptions
import com.cn.appcore.base.BaseVMActivity
import com.cn.appcore.event.RxBus
import com.cn.appcore.event.RxBusBean
import com.cn.appcore.event.SchedulerTransformer
import com.cn.appcore.ext.getCompatColor
import com.cn.appcore.ext.loge
import com.cn.appcore.ext.setSingleClickListener
import com.cn.appcore.ext.showToast
import com.cn.appcore.http.constant.HttpConstant
import com.cn.appcore.utils.GlideEngine
import com.cn.appcore.utils.GlideUtil
import com.cn.appcore.utils.Preference
import com.cn.appcore.utils.TimeUtils
import com.ghsc.yigou.live.R
import com.ghsc.yigou.live.api.ApiService
import com.ghsc.yigou.live.config.CustKey
import com.ghsc.yigou.live.config.CustSignKey
import com.ghsc.yigou.live.config.LoginHelper
import com.ghsc.yigou.live.config.UserManager
import com.ghsc.yigou.live.databinding.ActivityMainBinding
import com.ghsc.yigou.live.ext.gone
import com.ghsc.yigou.live.ext.visible
import com.ghsc.yigou.live.ui.activity.LiveCreateActivity
import com.ghsc.yigou.live.ui.view.CustLiveProtoType
import com.ghsc.yigou.live.utils.OssUpUtils
import com.ghsc.yigou.live.utils.StringUtil
import com.ghsc.yigou.live.view.ConfirmCancelPopViiew
import com.luck.picture.lib.PictureSelector
import com.luck.picture.lib.config.PictureConfig
import com.luck.picture.lib.config.PictureMimeType
import com.luck.picture.lib.entity.LocalMedia
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.enums.PopupAnimation
import com.permissionx.guolindev.PermissionX
import java.util.ArrayList

/**
 * 项目：YiGouLive
 * 包名：com.ghsc.yigou.live.ui
 * 类名：MainActivity
 * 创建时间：2024/8/1 10:36
 * 创建人 @author： xiaolin
 * 描述 ：Todo...
 */
class MainActivity : BaseVMActivity<MainViewModel, ActivityMainBinding>()  {
    override fun attachLayoutRes(): Int {
        return R.layout.activity_main
    }

    override fun attachVMClass(): Class<MainViewModel>? {
        return MainViewModel::class.java
    }
    var userId: String by Preference(HttpConstant.USER_ID, "")

    override fun initBar() {
        super.initBar()
        mImmersionBar!!.statusBarDarkFont(true).init()
    }
    override fun initView() {
        binding.bar.topLeftView.gone()
        binding.bar.topTitle.text = "直播"
    }





    override fun onResume() {
        super.onResume()


    }


    override fun initClickListener() {
        super.initClickListener()
        binding.tvBtnNext.setSingleClickListener {
            if ( !TextUtils.isEmpty(AppServerTokenManager.getAppServerToken()) && !TextUtils.isEmpty(
                    Const.getUserId()) &&
                Const.getUserId() == userId) {
                loge("直播登录了=")
                startActivity(LiveCreateActivity().javaClass)
                //todo 登录
            } else {
                showLoading()
                // 直播请求自己的接口 登录 login 参数 request
                AppServerTokenManager.login(userId, userId,
                    object : InteractionCallback<Void?> {
                        override fun onSuccess(data: Void?) {
                            loge("直播登录=")
                            dismissLoading()
                            Const.setUserId(userId)
                            if (!TextUtils.isEmpty(Const.getUserId())){
                                //todo 登录成功
                                startActivity(LiveCreateActivity().javaClass)
                            }
                        }
                        override fun onError(interactionError: InteractionError?) {
                            dismissLoading()
                            showToast("服务器出错...")
                        }
                    })
            }
        }
    }


}